package com.cetccst.securitysuite.fragments.viewmodel;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import androidx.work.BackoffPolicy;
import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.NetworkType;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;

import com.cetccst.securitysuite.log.SSLogger;
import com.cetccst.securitysuite.network.HttpCallback;
import com.cetccst.securitysuite.network.UserHttpRequester;
import com.cetccst.securitysuite.network.respone.UserResponse;
import com.cetccst.securitysuite.worker.UploadDataWorker;
import com.cetccst.securitysuite.worker.WorkerKey;

import java.time.Duration;

public class MainViewModel extends ViewModel {

    public MutableLiveData<String> userName = new MutableLiveData<>("张三");
    public MutableLiveData<String> userNumber = new MutableLiveData<>("牛马1号");
    private WorkManager workManager;
    private PeriodicWorkRequest periodicWork;

    public void postAppDataPeriodically() {
        workManager.enqueueUniquePeriodicWork(
                WorkerKey.UPLOAD_DATA.toString(),
                ExistingPeriodicWorkPolicy.REPLACE,
                periodicWork);
    }


    public void initialize(Fragment fragment) {

        workManager = WorkManager.getInstance(fragment.requireContext().getApplicationContext());

        periodicWork = new PeriodicWorkRequest.Builder(
                UploadDataWorker.class,
                Duration.ofHours(1)//repeatInterval
        )
                .setConstraints(new Constraints.Builder()
                        .setRequiredNetworkType(NetworkType.UNMETERED)
                        .build()
                )
                .setInitialDelay(Duration.ofMinutes(1))
                .setBackoffCriteria(
                        BackoffPolicy.EXPONENTIAL,
                        Duration.ofMinutes(5)
                )
//                        .setInputData(可以传递数据)
                .build();
    }

    public void getJsonUser() {
        UserHttpRequester.getInstance().getJsonUser(new HttpCallback<>() {
            @Override
            public void onResponse(UserResponse data) {
                SSLogger.logger.error("getJsonUser be called");
                if(data!=null) {
                    userName.postValue(data.getUser().getUserName());
                    userNumber.postValue(data.getUser().getUserNumber());
                }
            }
        });
    }
    public void getXmlUser() {
        UserHttpRequester.getInstance().getXmlUser(new HttpCallback<>() {
            @Override
            public void onResponse(UserResponse data) {
                SSLogger.logger.error("getXmlUser be called");
                if(data!=null) {
                    userName.postValue(data.getUser().getUserName());
                    userNumber.postValue(data.getUser().getUserNumber());
                }
            }
        });
    }
}
